#include "data_mange.h"
#include "ui_data_mange.h"

data_mange::data_mange(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::data_mange)
{
    ui->setupUi(this);
    this->setWindowTitle("档案管理");
    this->setStyleSheet("background-color: #E2EEF7");
    setAttribute(Qt::WA_QuitOnClose,false);
    set_menu();
    ui->tableWidget->setColumnCount(8);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("档案编号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("儿童ID"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("儿童信息"));
    ui->tableWidget->setHorizontalHeaderItem(3,new QTableWidgetItem("筛查结果"));
    ui->tableWidget->setHorizontalHeaderItem(4,new QTableWidgetItem("筛查时间"));
    ui->tableWidget->setHorizontalHeaderItem(5,new QTableWidgetItem("筛查详情"));
    ui->tableWidget->setHorizontalHeaderItem(6,new QTableWidgetItem("打印"));
    ui->tableWidget->setHorizontalHeaderItem(7,new QTableWidgetItem("删除详情"));
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //列宽自适应内容
    ui->tableWidget->resizeColumnsToContents();
      //隐藏表格的列标号
    ui->tableWidget->verticalHeader()->setVisible(false);

    //设置表格不可编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);


    sql = QSqlDatabase::addDatabase("QSQLITE", "seventh");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }


    connect(ui->pushButton_search, SIGNAL(clicked(bool)), this, SLOT(get_info()));



}








void data_mange::display_date(){

    this->show();
}

void data_mange::get_info(){
    id = ui->lineEdit_id->text();
    name = ui->lineEdit_name->text();
    phone = ui->lineEdit_phone->text();
    sex = ui->comboBox_sex->currentIndex() - 1;
    grade = ui->comboBox_grade->currentIndex() - 1;
    f_w = ui->comboBox_f_w->currentIndex() - 1;
    m_w = ui->comboBox_m_w->currentIndex() - 1;
    f_e = ui->comboBox_f_e->currentIndex() - 1;
    m_e = ui->comboBox_m_e->currentIndex() - 1;
    family = ui->comboBox_family->currentIndex() - 1;
    res = ui->comboBox_res->currentIndex() - 1;
    birth_low = ui->dateEdit_birth_low->text();
    birth_high = ui->dateEdit_birth_high->text();
//    date_low = ui->dateEdit_date_low->text();
//    date_high = ui->dateEdit_date_high->text();



    QSqlQuery query(sql);
    //执行sql查询操作
    QString sql_text = "SELECT * FROM child_data LEFT JOIN tb_result ON child_data.id=tb_result.child_id WHERE ";
    int tmp_len = sql_text.size();

    if(name.isEmpty() == false && name.isNull() == false){
        sql_text += "child_data.name='" + name + "' AND ";
    }
    if(phone.isEmpty() == false && phone.isNull() == false){
        sql_text += "child_data.phone='" + phone + "' AND ";
    }
    if(sex != -1){
        sql_text += "child_data.sex=" + QString::number(sex) + " AND ";
    }
    if(grade != -1){
        sql_text += "child_data.grade=" + QString::number(grade) + " AND ";
    }
    if(f_w != -1){
        sql_text += "child_data.Father_occ=" + QString::number(f_w) + " AND ";
    }
    if(m_w != -1){
        sql_text += "child_data.Mother_occ=" + QString::number(m_w) + " AND ";
    }
    if(f_e != -1){
        sql_text += "child_data.Father_edu_level=" + QString::number(f_e) + " AND ";
    }
    if(m_e != -1){
        sql_text += "child_data.Mother_edu_level=" + QString::number(m_e) + " AND ";
    }
    if(family != -1){
        sql_text += "child_data.family=" + QString::number(family) + " AND ";
    }


    if(id.isEmpty() == false && id.isNull() == false){
        sql_text += "tb_result.id=" + id + " AND ";
    }
    if(res == 0){
        sql_text += "tb_result.total>40 AND ";
    }
    if(res == 1){
        sql_text += "tb_result.total<40 AND ";
    }


    if(sql_text.size() > tmp_len){
         sql_text = sql_text.left(sql_text.size() - 5);
    }
    else{
        sql_text = sql_text.left(sql_text.size() - 7);
    }
    qDebug() << sql_text;


    query.exec(sql_text);

    int i = 0;
    while(query.next()){
        i++;
    }
    query.seek(-1);
    ui->tableWidget->setRowCount(i);

    i = 0;

    QPushButton* btn1 = NULL;
    QPushButton* btn2 = NULL;
    QPushButton* btn3 = NULL;
    while(query.next()){
        btn1 = new QPushButton(this);
        btn2 = new QPushButton(this);
        btn3 = new QPushButton(this);

        connect(btn1, SIGNAL(clicked(bool)), this, SLOT(click_check()));
        connect(btn2, SIGNAL(clicked(bool)), this, SLOT(click_print()));
        connect(btn3, SIGNAL(clicked(bool)), this, SLOT(click_delete()));

        btn1->setText("查看详情");
        btn2->setText("打印");
        btn3->setText("删除档案");
        btn1->setStyleSheet("background-color: #55AAFF; color: white");
        btn2->setStyleSheet("background-color: #00DD00; color: white");
        btn3->setStyleSheet("background-color: #C70063; color: white");

        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(query.value(12).toString()));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(query.value(0).toString()));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(query.value(1).toString()));

        if(query.value(20).toInt() > 40){
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem("通过"));
        }
        else{
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem("未通过"));
        }


        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(query.value(13).toString()));
        ui->tableWidget->setCellWidget(i,5, btn1);
        ui->tableWidget->setCellWidget(i,6, btn2);
        ui->tableWidget->setCellWidget(i,7, btn3);

        id_list.append(query.value(0).toString());
        i++;


    }


}

void data_mange::click_check(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    qDebug() << id.toInt();
    emit show_detail(id.toInt());
    this->hide();
}


void data_mange::click_print(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    qDebug() << id.toInt();
    emit want_print(id.toInt());
    this->hide();
}


void data_mange::click_delete(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    qDebug() << id.toInt();
    QSqlQuery query(sql);
    QString sql_text = "DELETE FROM tb_result WHERE child_id="  + id;
    query.exec(sql_text);
    sql_text = "DELETE FROM child_data WHERE id="  + id;
    query.exec(sql_text);
    QMessageBox::information(this, "删除成功", "删除成功", "确认", "取消");


}


void data_mange::set_menu(){

    sex_list << "不限" << "女" << "男";
    ui->comboBox_sex->addItems(sex_list);


    grade_list << "不限" << "一年级" << "二年级" << "三年级" << "四年级" << "五年级"\
               << "六年级" << "七年级" << "八年级" << "九年级";
    ui->comboBox_grade->addItems(grade_list);

    family_list << "不限" << "核心家庭" << "主干家庭" << "联合家庭" << "单亲家庭" << "重组家庭" << "丁克家庭";
    ui->comboBox_family->addItems(family_list);

    work_list.append(tr("不限"));
    work_list.append(tr("国家机关党群组织"));
    work_list.append(tr("专业技术人员"));
    work_list.append(tr("办事人员和有关人员"));
    work_list.append(tr("商业服务业人员"));
    work_list.append(tr("农林牧渔水利"));
    work_list.append(tr("生产运输"));
    work_list.append(tr("军人"));
    work_list.append(tr("不便分类"));
    work_list.append(tr("无职业"));
    ui->comboBox_f_w->addItems(work_list);
    ui->comboBox_m_w->addItems(work_list);

    edu_list.append(tr("不限"));
    edu_list.append(tr("博士"));
    edu_list.append(tr("硕士"));
    edu_list.append(tr("本科"));
    edu_list.append(tr("大专"));
    edu_list.append(tr("中专与中技"));
    edu_list.append(tr("高中"));
    edu_list.append(tr("初中"));
    edu_list.append(tr("小学"));
    edu_list.append(tr("文盲与半文盲"));
    ui->comboBox_f_e->addItems(edu_list);
    ui->comboBox_m_e->addItems(edu_list);

    res_list << "不限" << "通过" << "未通过";
    ui->comboBox_res->addItems(res_list);
}

data_mange::~data_mange()
{
    delete ui;
}
